package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.dto.StoreProjectsDto;
import com.dingreading.cloud.common.util.AccountUtil;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.dto.StoreProjectsDtoV2;
import com.dingreading.cloud.soms.entity.StoreProjects;
import com.dingreading.cloud.soms.entity.table.StoreProjectsTableDef;
import com.dingreading.cloud.soms.mapper.StoreProjectsMapper;
import com.dingreading.cloud.soms.service.StoreProjectsService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-09-11
 */
@Service
public class StoreProjectsServiceImpl extends BaseServiceImpl<StoreProjectsMapper, StoreProjects, StoreProjectsTableDef> implements StoreProjectsService {

    @Override
    protected StoreProjectsTableDef getTable() {
        return StoreProjectsTableDef.storeProjects;
    }


    @Override
    public Page<StoreProjectsDto> storeProjectPageV3(
            PageUtil pageUtil, String storeUid, String keyword, Integer lessonStatus, Integer freeStatus,
            Integer enabled, List<Integer> cooperationTypes, Integer operationType, List<Long> projectIdList
    ) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.projectName.like(keyword));
        if (null != lessonStatus)
            condition.and(table.lessonStatus.eq(lessonStatus));
        if (null != freeStatus)
            condition.and(table.freeStatus.eq(freeStatus));

        String cooperationTypeSql = "";
        if (CollectionUtils.isNotEmpty(cooperationTypes)) {
            cooperationTypeSql = AccountUtil.getCooperationTypeSql(cooperationTypes);
        }
        String operationTypeSql = "";
        if (operationType != null) {
            operationTypeSql = AccountUtil.getOperationTypeSql(operationType);
        }
        if (CollectionUtils.isNotEmpty(projectIdList)) {
            condition.and(table.id.in(projectIdList));
        }

        if (null == enabled) {
            condition.and(table.storeUid.isNull().and(table.source.eq(1)).and(table.enabled.eq(1)).and(cooperationTypeSql).and(operationTypeSql)
                    .or(table.storeUid.eq(storeUid).and(table.source.eq(2)))
            );
        } else {
            if (enabled.equals(1)) {
                condition.and(table.storeUid.isNull().and(table.source.eq(1)).and(table.enabled.eq(1)).and(cooperationTypeSql).and(operationTypeSql)
                        .or(table.storeUid.eq(storeUid).and(table.source.eq(2)).and(table.enabled.eq(1)))
                );
            } else {
                condition.and(table.storeUid.eq(storeUid).and(table.source.eq(2)).and(table.enabled.eq(0)));
            }
        }

        QueryWrapper wrapper = QueryWrapper.create()
                .select()
                .from(table)
                .where(condition)
                .orderBy(table.source.asc(), table.orderNum.asc());

        return getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, StoreProjectsDto.class);
    }

    @Override
    public Page<StoreProjectsDto> storeProjectPageV5(
            PageUtil pageUtil, String storeUid, String keyword, Integer lessonStatus, Integer freeStatus,
            Integer enabled, List<Long> projectIdList
    ) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.projectName.like(keyword));
        if (null != lessonStatus)
            condition.and(table.lessonStatus.eq(lessonStatus));
        if (null != freeStatus)
            condition.and(table.freeStatus.eq(freeStatus));
//        if (CollectionUtils.isNotEmpty(projectIdList)) {
//            condition.and(table.id.in(projectIdList));
//        }


        if (null == enabled) {
            condition.and(table.storeUid.isNull().and(table.source.eq(1)).and(table.enabled.eq(1)).and(table.id.in(projectIdList))
                    .or(table.storeUid.eq(storeUid).and(table.source.eq(2)))
            );
        } else {
            if (enabled.equals(1)) {
                condition.and(table.storeUid.isNull().and(table.source.eq(1)).and(table.enabled.eq(1)).and(table.id.in(projectIdList))
                        .or(table.storeUid.eq(storeUid).and(table.source.eq(2)).and(table.enabled.eq(1)))
                );
            } else {
                condition.and(table.storeUid.eq(storeUid).and(table.source.eq(2)).and(table.enabled.eq(0)));
            }
        }

        QueryWrapper wrapper = QueryWrapper.create()
                .select()
                .from(table)
                .where(condition)
                .orderBy(table.source.asc(), table.orderNum.asc());

        return getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, StoreProjectsDto.class);
    }

    @Override
    public boolean updateEnabledV2(String storeUid, String uid, Integer enabled, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.source.eq(2))
                .and(table.uid.eq(uid));

        return UpdateChain.of(StoreProjects.class)
                .set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public int dropBean(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid))
                .and(table.storeUid.eq(storeUid))
                .and(table.source.eq(2));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public boolean haveNameV2(String storeUid, String uid, String productName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.projectName.eq(productName));
        condition.and(
                table.storeUid.isNull().and(table.source.eq(1))
                        .or(table.storeUid.eq(storeUid)).and(table.source.eq(2))
        );
        if (StringUtils.isNotBlank(uid))
            condition.and(table.uid.ne(uid));

        return exists(condition);
    }

    @Override
    public StoreProjects getByUid(String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid));
    }

    @Override
    public List<StoreProjects> listByUids(List<String> uids) {
        return getMapper().selectListByCondition(table.uid.in(uids));
    }

    @Override
    public int getMaxOrderNum(String storeUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.source.eq(2));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.orderNum))
                .from(table)
                .where(condition);
        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);
        return NepUtil.nullToZero(integer);
    }

    @Override
    public StoreProjects getGtShowOrder(String storeUid, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderNum.gt(orderNum))
                .and(table.storeUid.eq(storeUid))
                .and(table.source.eq(2));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition).orderBy(table.orderNum.asc());
        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public StoreProjects getLtShowOrder(String storeUid, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderNum.lt(orderNum))
                .and(table.storeUid.eq(storeUid))
                .and(table.source.eq(2));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition).orderBy(table.orderNum.desc());
        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public boolean updateOrderNum(String storeUid, String uid, Integer orderNum, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.source.eq(2))
                .and(table.uid.eq(uid));

        return UpdateChain.of(StoreProjects.class)
                .set(table.orderNum, orderNum)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public StoreProjects getByStoreUidAndUid(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid))
                .and(table.storeUid.eq(storeUid));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public List<StoreProjectsDtoV2> dtoListByStoreUidAndSelfStoreV2(
            String storeUid, String keyword, Integer lessonStatus, Integer freeStatus,
            String cooperationTypeSql, String operationTypeSql,
            List<Long> projectIdList
    ) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.projectName.like(keyword));
        if (null != lessonStatus)
            condition.and(table.lessonStatus.eq(lessonStatus));
        if (null != freeStatus)
            condition.and(table.freeStatus.eq(freeStatus));
        if (CollectionUtils.isNotEmpty(projectIdList))
            condition.and(table.id.in(projectIdList));

        condition.and(table.storeUid.isNull().and(table.source.eq(1)).and(table.enabled.eq(1)).and(cooperationTypeSql).and(operationTypeSql)
                .or(table.storeUid.eq(storeUid).and(table.source.eq(2)).and(table.enabled.eq(1)))
        );

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.source.asc(), table.orderNum.asc());

        return getMapper().selectListByQueryAs(wrapper, StoreProjectsDtoV2.class);
    }

    @Override
    public List<StoreProjectsDtoV2> dtoListByStoreUidAndSelfStoreV5(String storeUid, String keyword, Integer lessonStatus, Integer freeStatus, List<Long> projectIdList) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.projectName.like(keyword));
        if (null != lessonStatus)
            condition.and(table.lessonStatus.eq(lessonStatus));
        if (null != freeStatus)
            condition.and(table.freeStatus.eq(freeStatus));
//        if (CollectionUtils.isNotEmpty(projectIdList))
//            condition.and(table.id.in(projectIdList));

        condition.and(table.storeUid.isNull().and(table.source.eq(1)).and(table.enabled.eq(1)).and(table.id.in(projectIdList))
                .or(table.storeUid.eq(storeUid).and(table.source.eq(2)).and(table.enabled.eq(1)))
        );

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.source.asc(), table.orderNum.asc());

        return getMapper().selectListByQueryAs(wrapper, StoreProjectsDtoV2.class);
    }

    @Override
    public StoreProjectsDto getDtoByUid(String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition);
        return getMapper().selectOneByQueryAs(wrapper, StoreProjectsDto.class);
    }

    @Override
    public boolean updateInfoV2(String storeUid, String uid, String projectName, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.source.eq(2))
                .and(table.uid.eq(uid));

        return UpdateChain.of(StoreProjects.class)
                .set(table.projectName, projectName)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public StoreProjects getByProjectName(String projectName) {
        return getMapper().selectOneByCondition(table.projectName.eq(projectName));
    }

}
